package com.krause.marinechart.iso8211;

import java.io.BufferedInputStream;
import java.io.IOException;

public class Record implements Constants{
	
	protected Leader mLeader;
	protected Directory mDirectory;
	protected Data mData;

	public Record (BufferedInputStream file) throws IOException {
		mLeader = getLeader(file);
		mDirectory = getDirectory(file);
		mData = getData(file);
		
		Module.sb.append('\n');
	}
	
	protected Leader getLeader (BufferedInputStream file) throws IOException {
		byte[] mLeaderArray = new byte[DDF_LEADER_LENGTH];
		file.read(mLeaderArray, 0, mLeaderArray.length);
		return new Leader(mLeaderArray);
	}
	
	protected Directory getDirectory (BufferedInputStream file) throws IOException {
		byte[] mDirectoryArray = new byte[mLeader.fieldAreaStart - DDF_LEADER_LENGTH];
		file.read(mDirectoryArray, 0, mDirectoryArray.length);
		return new Directory(mDirectoryArray, mLeader);
	}
	
	protected Data getData (BufferedInputStream file) throws IOException {
		byte[] mDataArray = new byte[mLeader.recLength - mLeader.fieldAreaStart];
		file.read(mDataArray, 0, mDataArray.length);
		return new Data(mDataArray, mDirectory);
	}
	
}
